package com.eclipse.training.ui;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;

import com.eclipse.training.Utils;
import com.eclipse.training.model.Galaxy;
import com.eclipse.training.model.GalaxyCluster;
import com.eclipse.training.model.Planet;
import com.eclipse.training.model.SolarSystem;
import com.eclipse.training.model.Star;
import com.eclipse.training.model.StarCluster;

public class TreeVoidFilter extends ViewerFilter {

	@Override
	public boolean select(Viewer viewer, Object parentElement, Object element) {
		if (element instanceof Galaxy) {
			Object[] s = ((Galaxy) element).getStars().toArray();
			Object[] sC = ((Galaxy) element).getStarClusters().toArray();
			Object[] n = ((Galaxy) element).getNebulae().toArray();
			return Utils.concat(s, Utils.concat(sC, n)).length > 0;
		} else if (element instanceof GalaxyCluster) {
			return ((GalaxyCluster) element).getGalaxies().size() > 0;
		} else if (element instanceof Planet) {
			return ((Planet) element).getSatellites().size() > 0;
		} else if (element instanceof SolarSystem) {
			Object[] a = ((SolarSystem) element).getAsteroids().toArray();
			Object[] p = ((SolarSystem) element).getPlanets().toArray();
			Object[] s = ((SolarSystem) element).getStars().toArray();
			return Utils.concat(a, Utils.concat(p, s)).length > 0;
		} else if (element instanceof Star) {
			return ((Star) element).getSystems() != null;
		} else if (element instanceof StarCluster) {
			if (((StarCluster) element).getStars() != null)
				return ((StarCluster) element).getStars().size() > 0;
			else
				return false;
		}
		return true;
	}

}
